package com.thinkTank.oa.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.StringUtil;
import com.thinkTank.oa.base.BaseServiceImpl;
import com.thinkTank.oa.common.Constant;
import com.thinkTank.oa.dao.ProjectAttrMapper;
import com.thinkTank.oa.dao.ProjectMapper;
import com.thinkTank.oa.entity.Customer;
import com.thinkTank.oa.entity.Demand;
import com.thinkTank.oa.entity.Project;
import com.thinkTank.oa.entity.ProjectAttr;
import com.thinkTank.oa.service.ProjectService;
import com.thinkTank.oa.util.FileUtils;

@Transactional
@Service("projectService")
public class ProjectServiceImpl extends BaseServiceImpl<Project> implements ProjectService {
    @Autowired
    private ProjectAttrMapper projectAttrMapper;
    private ProjectMapper projectMapper;

    @Autowired  
    public void setProjectMapper(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
        super.setBaseMapper(projectMapper);
    }
    
    @Override
    public Project findByProjectNumber(int projectNumber) {
        return projectMapper.findByProjectNumber(projectNumber);
    }
    
    
    @Override
    public void save(Project project, ProjectAttr projectAttr, MultipartFile file) {
    	//保存文件，没有附件不会保存
    	String fileName = FileUtils.transferTo(Constant.DEF_PROJECT_PATH, file, "project");
        project.setAnnex(fileName);
        add(project);
        projectAttrMapper.add(projectAttr);
    }
    
    @Override
    public void update(Project project, ProjectAttr projectAttr, MultipartFile file) {
    	String fileName = FileUtils.transferTo(Constant.DEF_PROJECT_PATH, file, "project");
    	//如果没有附件则不改变原文件地址
    	if (StringUtil.isNotEmpty(fileName))
    		project.setAnnex(fileName);
        update(project);
        projectAttrMapper.update(projectAttr);
    }
    

    @Override
    public void updateProjectHolder(Project project) {
        projectMapper.updateProjectHolder(project);
    }

    @Override
    public void importData(Project project, Consumer<Project> action) {
    	action.accept(project);
    }
    @Override
    public List<Map<Object,Object>> findProjectByIndex(Integer start){
    	return projectMapper.findProjectByIndex(start);
    }
    
    @Override
    public void updateProjectContact(Project project) {
    	projectMapper.updateProjectContact(project);
    }
    
    @Override
    public List<Demand> queryButtedDemand(String projectId) {
    	return projectMapper.queryButtedDemand(projectId);
    }
    
    /**
	 * 通过项目联系人查询项目
	 * @author dongli
	 * @date 2018年11月5日
	 * @param project
	 * @return
	 */
	public List<Project> findProjectByProjectContactId(Project project){
		return projectMapper.findProjectByProjectContactId(project);
	}
	
	@Override
	public List<Customer> queryCustomer(String projectId) {
		return projectMapper.queryCustomer(projectId);
	}
}
